
//
// 120121 - AH - (Garage) Created.
// 120129 - AH - (Midtun) Got away with Set button and reflect state of passing 
//               strategy.
// 120131 - AH - (Huset *) Fixed that no matter what always the default
//               WheelPassingStrategy was sat.
// 120205 - AH - (Bien) Moved away controls here to type ontologies in the
//               sequence in a forward looking way to 
//               OntologySequenceDefiningPanel. Now this is a SequenceReceiver
//               so is told what was selected in a 
//               OntologySequenceDefiningPanel. 
// 120209 - AH - Now gets passing strategy from SoekratEngine.
// 120321 - AH - Removed @Override for interface method (1.5 compatible).
//

package org.soekrat.gui.swing.motor.wheelpassingstrategy;

import static org.soekrat.gui.swing.motor.passingstrategy.PassingStrategyUtil.getOntologyClassForCname;
import static org.soekrat.gui.swing.motor.passingstrategy.PassingStrategyUtil.getOntologyClassNameForUname;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import org.soekrat.engine.Soekrat2Engine;
import org.soekrat.engine.motor.Motor;
import org.soekrat.engine.motor.passingstrategies.WheelPassingStrategy;
import org.soekrat.gui.swing.motor.passingstrategy.SequenceReceiver;

/**
 * <p>
 *   Panel to specify a
 *   {@link WheelPassingStrategy}.
 * </p>
 * @author Laetitia Le Chatton (LLC) 
 * @author Arne Halvorsen (AH)
 */
@SuppressWarnings("serial")
public final class WheelPassingStrategyPanel extends JPanel 
  implements SequenceReceiver 
{
  private final Soekrat2Engine m_engine;
  
  private final JTextField m_ontologies=new JTextField(30);
  
  private final JTextField m_turns=new JTextField(3);
  
  /**
   * <p>
   *   Constructor.
   * </p>
   * @param se {@link Soekrat2Engine} interfaced.
   */
  public WheelPassingStrategyPanel(Soekrat2Engine se)
  {
    if (se==null)
    {
      throw new NullPointerException("se");
    }
    
    m_engine=se;
    
    setLayout(new BorderLayout());
    
    JPanel center=new JPanel(new FlowLayout(FlowLayout.LEFT));
    
    center.add(new JLabel(" Pass "));
    center.add(m_ontologies);
    center.add(new JLabel("Turns: "));
    center.add(m_turns);
    
    m_turns.addActionListener
    (
      new ActionListener()
      {
        public void actionPerformed(ActionEvent e){ set(); }
      }
    );
    
    add(center, BorderLayout.CENTER);
    
    JPanel south=new JPanel(new FlowLayout(FlowLayout.LEFT));
    south.add(new JLabel("Ontology: "));
    
    init();
  }
  
  @Override
  public void setEnabled(boolean v)
  {
    m_ontologies.setEnabled(v);
    m_turns.setEnabled(v);
  }
  
  private WheelPassingStrategy getPassingStrategy()
  {
    WheelPassingStrategy ps=
      (WheelPassingStrategy)m_engine.getPassingStrategy(
        WheelPassingStrategy.class);
      
    if (ps==null)
    {
      ps=new WheelPassingStrategy();
      m_engine.addPassingStrategy(ps);
    }
    
    return ps;
  }
  
  private void init()
  {   
    m_ontologies.setText("");
    m_turns.setText("");
     
    WheelPassingStrategy ps=getPassingStrategy();
    
    m_engine.getMotor().setPassingStrategy(ps);
     
    int turns=ps.getTurns();
    m_turns.setText(Integer.toString(turns));
      
    String[] names=ps.getSeguence();
      
    int n=names.length;
    StringBuilder sb=new StringBuilder();
    for (int i=0; i<n; i++)
    {
      if (i>0) sb.append(", ");
      String name=names[i];
      String uname=getOntologyClassForCname(name);
      sb.append(uname==null ? name : uname);
    }
    m_ontologies.setText(sb.toString());
  }
  
  private void set()
  {
    WheelPassingStrategy wps=new WheelPassingStrategy();
    
    Motor motor=m_engine.getMotor();
    
    String txt=m_turns.getText();
    int turns=Integer.parseInt(txt);
    wps.setTurns(turns);
    
    List<String> seq=new ArrayList<String>();
    
    txt=m_ontologies.getText();   
    
    StringTokenizer st=new StringTokenizer(txt, ",");
    
    while (st.hasMoreTokens())
    {
      String uname=st.nextToken();
      
      String cname=getOntologyClassNameForUname(uname);
      if (cname!=null)
      {
        seq.add(cname);
      }
    }
    wps.setSequence(seq);
    
    m_ontologies.setText("");
    
    motor.setPassingStrategy(wps);
    
    init();
  }
  
  // Defined in SequenceReceiver.
  public String getSequence(){ return m_ontologies.getText(); }

  // Defined in SequenceReceiver.
  public void sequence(String txt){ m_ontologies.setText(txt); set(); }
  
}
